package com.YxyBackMange.Controller.Ware;


import com.YxyBackMange.Constant.DictTypeConstant;
import com.YxyBackMange.Dto.Message;

import com.YxyBackMange.Entity.Ware.*;
import com.YxyBackMange.Mapper.Ware.*;

import com.YxyBackMange.Entity.App.AppDictCode;
import com.YxyBackMange.Entity.App.AppDictCodeType;
import com.YxyBackMange.Entity.Ware.WareGoodsInfo;
import com.YxyBackMange.Mapper.App.AppDictCodeMapper;
import com.YxyBackMange.Mapper.App.AppDictCodeTypeMapper;
import com.YxyBackMange.Mapper.Ware.WareGoodsInfoMapper;

import com.YxyBackMange.Service.Ware.WareGoodsInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Admin/Ware/WareGoodsInfo")
public class  WareGoodsInfoController {

    private Logger logger= LoggerFactory.getLogger(WareGoodsInfoController.class);

    @Autowired
    private WareGoodsInfoService wareGoodsInfoService;

    @Autowired
    private  WareGoodsInfoMapper wareGoodsInfoMapper;

    @Autowired
    private WareGoodsBrandMapper wareGoodsBrandMapper;
    @Autowired
    private WareGoodsTypeMapper wareGoodsTypeMapper;
    @Autowired
    private WareGoodsImgMapper wareGoodsImgMapper;
    @Autowired
    private WareGoodsAttrMapper wareGoodsAttrMapper;
    @Autowired
    private WareGoodsDetailsMapper wareGoodsDetailsMapper;

    @Autowired
    private AppDictCodeTypeMapper appDictCodeTypeMapper;

    @Autowired
    private AppDictCodeMapper appDictCodeMapper;

    @RequestMapping("/listPage")
    public String listPage(HttpSession session, ModelMap map){

        AppDictCodeType appDictCodeType = new AppDictCodeType();
        appDictCodeType.setTypeKey(DictTypeConstant.ORDERSTATE);
        appDictCodeType = appDictCodeTypeMapper.selectOne(appDictCodeType);
        AppDictCode appDictCode = new AppDictCode();
        appDictCode.setDictTypeId(appDictCodeType.getId());
        map.put("brandList", wareGoodsBrandMapper.selectAll());
        return "/admin/page/Ware/WareGoodsInfo/listWareGoodsInfoPage.jsp";

    }
    @RequestMapping("/addPage")
    public String addPage(HttpSession session, ModelMap map){
        map.put("brandList", wareGoodsBrandMapper.selectAll());
        WareGoodsType wareGoodsType = new WareGoodsType();
        wareGoodsType.setGrade(2);
        map.put("gradeList", wareGoodsTypeMapper.select(wareGoodsType));
        return "/admin/page/Ware/WareGoodsInfo/updateWareGoodsInfoPage.jsp";
    }
    @RequestMapping("/detailPage")
    public String detailPage(HttpSession session, @RequestParam Integer id, ModelMap map){
        WareGoodsInfo wareGoodsInfo=new WareGoodsInfo(id);
        wareGoodsInfo=wareGoodsInfoMapper.selectByPrimaryKey(wareGoodsInfo);
        map.put("obj",wareGoodsInfo);
        return "/admin/page/Ware/WareGoodsInfo/detailWareGoodsInfoPage.jsp";

    }
    @RequestMapping("/updatePage")
    public String updatePage(HttpSession session, @RequestParam Integer id, ModelMap map){
        map.put("brandList", wareGoodsBrandMapper.selectAll());
        WareGoodsType wareGoodsType = new WareGoodsType();
        wareGoodsType.setGrade(2);
        map.put("gradeList", wareGoodsTypeMapper.select(wareGoodsType));
        WareGoodsInfo wareGoodsInfo=new WareGoodsInfo(id);
        wareGoodsInfo=wareGoodsInfoMapper.selectByPrimaryKey(wareGoodsInfo);
        WareGoodsImg wareGoodsImg = new WareGoodsImg();
        wareGoodsImg.setGoodsId(wareGoodsInfo.getId());
        wareGoodsImgMapper.select(wareGoodsImg);
        map.put("obj",wareGoodsInfo);
        map.put("goodsImgList", wareGoodsImgMapper.select(wareGoodsImg));
        WareGoodsAttr wareGoodsAttr = new WareGoodsAttr();
        wareGoodsAttr.setGoodsId(wareGoodsInfo.getId());
        List<WareGoodsAttr> wareGoodsAttrList = wareGoodsAttrMapper.select(wareGoodsAttr);
        String goodsAtt = "";
        for (WareGoodsAttr wat : wareGoodsAttrList) {
            goodsAtt = wat.getAttrName() + ":" + wat.getAttrValue() + ";";
        }
        WareGoodsDetails wareGoodsDetails = new WareGoodsDetails();
        wareGoodsDetails.setId(wareGoodsInfo.getDetailsId());
        map.put("goodsAtt", goodsAtt);
        map.put("goodsDetail", wareGoodsDetailsMapper.selectByPrimaryKey(wareGoodsDetails));
        map.put("isUpdate", true);
        return "/admin/page/Ware/WareGoodsInfo/updateWareGoodsInfoPage.jsp";
    }
    @RequestMapping("/addOrUpdate")
    public String addOrUpdate(WareGoodsInfo wareGoodsInfo, String goodsAttr, String goodsDetail, HttpServletRequest request, HttpSession session)
    {
        Message msg=new Message();
        try{
            String[] goodsImg = request.getParameterValues("goodsImg");
            List<WareGoodsImg> goodsImgList = new ArrayList<WareGoodsImg>();
            for (int i = 0; i < goodsImg.length; i++) {
                WareGoodsImg wareGoodsImg = new WareGoodsImg();
                wareGoodsImg.setImgAddress(goodsImg[i]);
                goodsImgList.add(wareGoodsImg);
            }
            wareGoodsInfo.setGoodsImgList(goodsImgList);
            String[] goodsAttrArra = goodsAttr.split(";");
            List<WareGoodsAttr> wareGoodsAttrList = new ArrayList<WareGoodsAttr>();
            for (int i = 0; i < goodsAttrArra.length; i++) {
                WareGoodsAttr wareGoodsAttr = new WareGoodsAttr();
                String attr[] = goodsAttrArra[i].split(":");
                wareGoodsAttr.setAttrName(attr[0]);
                wareGoodsAttr.setAttrValue(attr[1]);
                wareGoodsAttrList.add(wareGoodsAttr);
            }
            wareGoodsInfo.setWareGoodsAttrList(wareGoodsAttrList);
            WareGoodsDetails wareGoodsDetails = new WareGoodsDetails();
            wareGoodsDetails.setContent(goodsDetail);
            wareGoodsInfo.setGoodsDetails(wareGoodsDetails);
            if(wareGoodsInfo!=null&&wareGoodsInfo.getId()!=null)
            {
                 wareGoodsInfo=wareGoodsInfoService.updateService(wareGoodsInfo);
            }else{

                  wareGoodsInfo=wareGoodsInfoService.insertService(wareGoodsInfo);
            }
            if(wareGoodsInfo==null)
            {
                msg.setMsg("操作失败,您可以稍后重试!");
                return "admin/page/Ware/WareGoodsInfo/error.html";
            }
            msg.setData(wareGoodsInfoMapper.selectQueryById(wareGoodsInfo.getId()));
            msg.setMsg("保存成功！");
            msg.setState(true);
            logger.info("保存成功:"+msg);
            return "admin/page/Ware/WareGoodsInfo/success.html";
        }catch (Exception e)
        {
            msg.setMsg("保存失败，请稍后重试");
            e.printStackTrace();
            logger.error("保存失败:"+e.getMessage());
            return "admin/page/Ware/WareGoodsInfo/success.html";
        }
    }
    @RequestMapping("/update")
    @ResponseBody
    public Message update(WareGoodsInfo wareGoodsInfo,HttpSession session)
    {
        Message msg=new Message();
        try{
            wareGoodsInfoService.update(wareGoodsInfo);
            msg.setData(wareGoodsInfoMapper.selectByPrimaryKey(wareGoodsInfo));
            msg.setMsg("更新成功！");
            msg.setState(true);
             logger.info("更新成功:"+msg);
            return msg;
        }catch (Exception e)
        {
            msg.setMsg("更新失败，请稍后重试");
            e.printStackTrace();
            logger.error("更新失败:"+e.getMessage());
            return msg;
        }
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Message delete(@RequestParam Integer id, HttpSession session)
    {
        Message msg=new Message();
        try{
            WareGoodsInfo wareGoodsInfo=new WareGoodsInfo(id);
            wareGoodsInfoService.delete(wareGoodsInfo);
            msg.setData(wareGoodsInfo);
            msg.setMsg("删除成功！");
            msg.setState(true);
            logger.error("删除成功:"+msg);
            return msg;
        }catch (Exception e)
        {
            msg.setMsg("删除失败，请稍后重试");
            logger.error("更新失败:"+e.getMessage());
            e.printStackTrace();
            return msg;
        }
    }
}
